<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="data"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      stripe
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >

      <el-table-column
        type="index"
        width="50"
      />

      <el-table-column
        align="center"
        prop="id"
        label="ID"
      />

      <el-table-column
        prop="name"
        label="类别名称"
        width="150"
        align="center"
      />

      <el-table-column
        prop="info"
        label="类别描述"
        width="150"
        align="center"
      />

      <el-table-column
        prop="dateCreated"
        label="创建时间"
        width="120"
        align="center"
      />

      <el-table-column
        prop="dateModified"
        label="修改时间"
        width="120"
        align="center"
      />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>

          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      background
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>

<script>
export default {
  data() {
    return {
      list: null,
      page: 1,
      limit: 10,
      total: 0,
      data: [{
        id: '0000001',
        name: '食品',
        info: '零食，面包，泡面7月9折',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000002',
        name: '饮品',
        info: '夏季可乐啤酒买4送1',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000003',
        name: '文具',
        info: '参加高考的学生可凭借学生证领取文具',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000004',
        name: '生活用品',
        info: '无',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData(page = 1) {
      this.page = page
      // eslint-disable-next-line no-undef
      product.getAll(this.url, this.page, this.limit)
        .then(response => {
          console.log(response)
          this.list = response.data.userList
          this.total = response.data.number
        })
        .catch(error => {
          console.log(error)
        })
    }
  },
  tableRowClassName({ row, rowIndex }) {
    if (rowIndex === 1) {
      return 'warning-row'
    } else if (rowIndex === 3) {
      return 'success-row'
    }
    return ''
  }
}
</script>
